package com.one.efaimaly.login.activity;

import android.Manifest;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Handler;
import android.support.v4.app.FragmentActivity;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.imnjh.imagepicker.CapturePhotoHelper;
import com.one.common.config.CPersisData;
import com.one.common.config.RouterPath;
import com.one.common.manager.RouterManager;
import com.one.common.utils.Logger;
import com.one.common.utils.StringUtils;
import com.one.common.utils.Toaster;
import com.one.common.view.base.BaseActivity;
import com.one.efaimaly.R;
import com.tbruyelle.rxpermissions2.RxPermissions;

import cn.jpush.android.api.JPushInterface;
import io.reactivex.functions.Consumer;

import static android.Manifest.permission.ACCESS_COARSE_LOCATION;
import static android.Manifest.permission.ACCESS_FINE_LOCATION;
import static android.Manifest.permission.CAMERA;
import static android.Manifest.permission.READ_EXTERNAL_STORAGE;
import static android.Manifest.permission.READ_PHONE_STATE;
import static android.Manifest.permission.WRITE_EXTERNAL_STORAGE;
import static android.Manifest.permission_group.LOCATION;

/**
 * creator: zhulunjun
 * time:    2018/8/16
 * describe:
 */
@Route(path = RouterPath.LOADING)
public class LoadingActivity extends BaseActivity {
    @Override
    protected void initPresenter() {

    }

    @Override
    public int getLayoutResId() {
        return R.layout.activity_loading;
    }

    @Override
    protected void initTitle() {
        super.initTitle();
        noHasTitle();
    }

    @Override
    public void initData() {
        super.initData();
        requestPermission();
        //保存极光id
        CPersisData.setJpushId(this);
    }


    @SuppressLint("CheckResult")
    private void requestPermission() {
        RxPermissions rxPermissions = new RxPermissions(this);
        rxPermissions.request(
                ACCESS_FINE_LOCATION,
                ACCESS_COARSE_LOCATION,
                READ_PHONE_STATE,
                CAMERA,
                WRITE_EXTERNAL_STORAGE,
                READ_EXTERNAL_STORAGE)
                .subscribe(new Consumer<Boolean>() {
                    @Override
                    public void accept(Boolean aBoolean) throws Exception {
                        if (aBoolean) { // 在android 6.0之前会默认返回true
                            goMain();
                        } else {
                            // 未获取权限
                            Toaster.showShortToast("您没有授权权限，请在设置中打开授权");
                        }
                    }
                });
    }

    private void goMain(){
        new Handler().postDelayed(new Runnable(){
            public void run() {
                if(CPersisData.getIsLogin()){
                    RouterManager.getInstance().goAndFinish(LoadingActivity.this,RouterPath.MAIN_PAGE);
                } else {
                    RouterManager.getInstance().goAndFinish(LoadingActivity.this,RouterPath.LOGIN_PAGE);
                }
            }
        }, 2*1000);
    }
}
